#  Copyright 2008-2012 Nokia Siemens Networks Oyj
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

'''
Define proxy objects for use in listener API 3

'''

# TODO: replace self._suite, self._testcase, self._keyword with self.__suite, 
# self.__testcase, self.__keyword. 

class ObjectProxy(object):
    def __str__(self):
        return self.longname
    def __repr__(self):
        return self.longname

class SuiteProxy(ObjectProxy):
    def __init__(self, suite, parent=None):
        self._suite = suite
        self.parent = self._suite.parent
        self.name = self._suite.name
        self.longname = self._suite.longname
        self.source = self._suite.source

class TestCaseProxy(ObjectProxy):
    def __init__(self, testcase, parent=None):
        self._testcase = testcase
        self.parent = parent
        self.name = self._testcase.name
        self.longname = ".".join((self.parent.longname, self.name))
        self.source = self.parent.source
        self.linenumber = getattr(self._testcase, "linenumber", None)
        self.steps = [KeywordProxy(keyword, self) for keyword in testcase.keywords]

class KeywordProxy(ObjectProxy):
    def __init__(self, keyword, parent=None):
        self._keyword = keyword
        self.parent = parent
        self.name = self._keyword.name
        self.longname = self._keyword.name
        self.linenumber = getattr(self._keyword, "linenumber", None)
#        import sys, pdb; pdb.Pdb(stdout=sys.__stdout__).set_trace()
#        self.steps = [KeywordProxy(keyword, self) for keyword in keyword.keywords]

    @property
    def source(self):
        if self.parent is None:
            # this should never be true, but just in case...
            return None
        return self.parent.source
